﻿using Bentley.CifNET.GeometryModel.SDK;
using Bentley.CifNET.Model;
using Bentley.CifNET.Objects;
using Bentley.CifNET.Objects.DataAccess;
using Bentley.CifNET.SDK.Edit;
using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.Internal.MstnPlatformNET.DotNetShim;
using CheccCoverplateCulvertAssemble.Common;
using CheccCoverplateCulvertAssemble.commons;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CheccCoverplateCulvertAssemble.util
{
    class SelectAlignmentTool : DgnElementSetTool
    {
        ConsensusConnectionEdit con = ConsensusConnectionEdit.GetActive();
        private List<AlignmentProperty> alignmentProperties = new List<AlignmentProperty>();
        public delegate void SetLineDatas(List<AlignmentProperty> alignments);
        private SetLineDatas _SetLineDatas;
        private Form seachForm;
        public SelectAlignmentTool(Form form, SetLineDatas setLineDatas)
        {
            seachForm = form;
            _SetLineDatas = setLineDatas;
        }
        public SelectAlignmentTool()
        {
        }

        protected override void ExitTool()
        {
            if (seachForm.IsDisposed == false)
            {
                seachForm.TopMost = true;
                seachForm.Show();
            }
            base.ExitTool();
        }
        public override StatusInt OnElementModify(Element element)
        {
            throw new NotImplementedException();
        }

        protected override bool OnPostLocate(HitPath path, out string cantAcceptReason)
        {
            
            cantAcceptReason = System.String.Empty;
            try
            {
                Element el = path.GetCursorElement();
                if (el != null)
                {
                    Alignment alignment = (el.ParentElement == null) ? Alignment.CreateFromElement(con, el) : Alignment.CreateFromElement(con, el.ParentElement);
                    if (alignment == null)
                        return false;
                    if (alignment.Stationing == null)
                        return false;
                }
            }
            catch (Exception e)
            {
                string msg = e.ToString();
            }
            return true;
        }

        protected override void OnRestartTool()
        {
            InstallNewInstance();
        }

        public static void InstallNewInstance()
        {
            SelectAlignmentTool tool = new SelectAlignmentTool();
            tool.InstallTool();
        }

        protected override void OnPostInstall()
        {
            //base.BeginPickElements();
            //开启“精确捕捉”的状态。
            Bentley.DgnPlatformNET.AccuSnap.SnapEnabled = true;
            //开启“精确捕捉”的自动定位功能。
            Bentley.DgnPlatformNET.AccuSnap.LocateEnabled = true;
            //设置捕捉方式为最近点
            Bentley.MstnPlatformNET.Settings.SnapMode = SnapMode.Nearest;
            //base.OnPostInstall();
            NotificationManager.OutputPrompt("选择主线");
        }

        protected override bool OnDataButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
        {
            Bentley.DgnPlatformNET.HitPath hitPath = DoLocate(ev, true, 1);
            if (hitPath == null)
                return false;

            Element el = hitPath.GetCursorElement();
            if (el == null)
                return false;
            Alignment al = (el.ParentElement == null) ? Alignment.CreateFromElement(con, el) : Alignment.CreateFromElement(con, el.ParentElement);


            List<string> corNames = new List<string>();
            GeometricModel gm = con.GetActiveGeometricModel();
            foreach (Corridor cor in gm.Corridors)
            {
                Alignment align = cor.CorridorAlignment;  //获取廊道对应路线
                if (align == null) continue;
                if (align.LinearGeometry.Equals(al.LinearGeometry)) //判断路线是否相等
                {
                    corNames.Add(cor.Name);
                }
            }
            //IObjectSpace objectSpase = RepositoryConnectionManager.Instance.GetActive().ObjectSpace;
            //Bentley.CifNET.GeometryModel.GeometricModel geometricModel = Bentley.CifNET.GeometryModel.GeometricModel.GetGeometricModel(objectSpase, GetModelOptions.AllowDoNotExist);

            //Bentley.CifNET.GeometryModel.LinearEntity2dInPlan mainLinear = Conmmon.ORDHelp.GetAllAlignmentsInModel(geometricModel).FirstOrDefault(p => p.Name == al.Name);

            //var ret = new List<Corridor>();
            //// 筛选主线设计廊道
            //var primaryCorridors = CheccCulvertHelpers.CivilHelper.GetCorridorListByAlignmentAndDisAlongRange(mainLinear, 1000, 1000, geometricModel);
            //foreach (var item in primaryCorridors)
            //{
            //    string sss = item.Name;
            //}

            if (corNames.Count == 0) MessageBox.Show("检测到该路线没有廊道!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
            
            if (al == null)
                return false;
            var aligmentInfo = AligmentHomologousProperties.GetAlignmentProperties(al);

            alignmentProperties.Add(aligmentInfo);

            NotificationManager.OutputPrompt("请按顺序选择路线");

            var stakeMarkData = StakeMarkTransistion();
            _SetLineDatas(alignmentProperties);
            if (alignmentProperties.Count == 3)
            {
                ExitTool();
            }
            return true;
        }
        List<string> StakeMarkTransistion()
        {
            List<string> strings = new List<string>();
            string sta = null;
            string end = null;
            for (int i = 0; i < alignmentProperties.Count; i++)
            {
                switch (i)
                {
                    case 0:
                        sta = StationConvert.ConvertZH(alignmentProperties[0].StationPrefix, alignmentProperties[0].StaStation);
                        end = StationConvert.ConvertZH(alignmentProperties[0].StationPrefix, alignmentProperties[0].EndStation);
                        strings.Add(sta + "~" + end);
                        break;
                    case 1:
                        sta = StationConvert.ConvertZH(alignmentProperties[1].StationPrefix, alignmentProperties[1].StaStation);
                        end = StationConvert.ConvertZH(alignmentProperties[1].StationPrefix, alignmentProperties[1].EndStation);
                        strings.Add(sta + "~" + end);
                        break;
                    case 2:
                        sta = StationConvert.ConvertZH(alignmentProperties[1].StationPrefix, alignmentProperties[1].StaStation);
                        end = StationConvert.ConvertZH(alignmentProperties[1].StationPrefix, alignmentProperties[1].EndStation);
                        strings.Add(sta + "~" + end);
                        break;
                }
            }
            return strings;
        }

        protected override bool OnResetButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
        {
            CommonData.Instance.CommomTotal.SelectAlignments.Clear();
            CommonData.Instance.CommomTotal.SelectAlignments = alignmentProperties;
            ExitTool();
            return true;
        }

        public void ClearAligmentData()
        {
            alignmentProperties.Clear();
        }
    }
}
